<?php
class KeywordHitLogsController extends AppController {

	var $name = 'KeywordHitLogs';
        var $uses = array("KeywordHitLog","Keyword");
	var $helpers = array('Html', 'Form',"FlashChart");

    function beforeFilter() {
                $this->set('page_title','Keywords Hits Management');
                parent::beforeFilter();
                        $this->Auth->allow('chart');
        }
        function reportcharts(){
                $this->KeywordHitLog->recursive = 0;
                $this->KeywordHitLog->unbindModel(array("belongsTo"=>array("Keyword")),false);           
                App::import('Core', 'Sanitize');
                $conditions = array();
                $conditions['User.id'] = $this->Auth->User('id');

                if(!empty($this->data)) {
                        if(!empty($this->data['KeywordHitLog']['fromDate']) && !empty($this->data['KeywordHitLog']['toDate'])) {
                             $conditions['KeywordHitLog.hit_date BETWEEN ? AND ?'] =  array($this->data['KeywordHitLog']['fromDate'],$this->data['KeywordHitLog']['toDate']);
                        }
                        if(!empty($this->data['KeywordHitLog']['website_id'])) {
                            $conditions['Website.id'] = $this->data['KeywordHitLog']['website_id'];
                        }

               }

                $hitdata = $this->KeywordHitLog->find('all', array(
                                  'conditions'=>$conditions,
                                    'fields'=>array("KeywordHitLog.*,SUM(KeywordHitLog.hit_count) as total"),
                                    'joins' => array(
                                        array('table' => 'keywords', 'alias' => 'Keyword', 'type' => 'left', 'conditions' => array('KeywordHitLog.keyword_id = Keyword.id')),
                                        array('table' => 'websites', 'alias' => 'Website', 'type' => 'left', 'conditions' => array('Keyword.website_id = Website.id')),
                                        array('table' => 'users', 'alias' => 'User', 'type' => 'left', 'conditions' => array('Website.user_id = User.id'))
                                        
                                    ),'group'=>array('DATE(KeywordHitLog.hit_date)')

                                ));
               // pr($hitdata);die();
		$this->set('keywordHitLogs',$hitdata);
             $websites = $this->Keyword->Website->find('list',array("fields"=>array("Website.id","Website.website_name"),"conditions"=>array("Website.user_id"=>$this->Auth->User('id'))));
             $this->set(compact('websites'));
        }
	function index() {
		$this->KeywordHitLog->recursive = 0;
                $this->KeywordHitLog->unbindModel(array("belongsTo"=>array("Keyword")),false);           
                App::import('Core', 'Sanitize');
                $conditions = array();
                $conditions['User.id'] = $this->Auth->User('id');
                if (isset($this->params['named']['q'])) {
                          $q=Sanitize::clean($this->params['named']['q']);
                          $this->set('q',$q);
                          $conditions['Keyword.keyword'] = $q;
                }
                  if (isset($this->params['named']['website'])) {
                        $website =Sanitize::clean($this->params['named']['website']);
                        $conditions['Website.id'] = $website;
                 }
                $this->paginate = array(
                                  'conditions'=>$conditions,
                                    'fields'=>array("User.id","Website.website_name","Website.id","Keyword.keyword","Keyword.campaign_reference","KeywordHitLog.*"),
                                    'joins' => array(
                                        array('table' => 'keywords', 'alias' => 'Keyword', 'type' => 'left', 'conditions' => array('KeywordHitLog.keyword_id = Keyword.id')),
                                        array('table' => 'websites', 'alias' => 'Website', 'type' => 'left', 'conditions' => array('Keyword.website_id = Website.id')),
                                        array('table' => 'users', 'alias' => 'User', 'type' => 'left', 'conditions' => array('Website.user_id = User.id')))

                                );
		$this->set('keywordHitLogs', $this->paginate('KeywordHitLog'));
                 $websites = $this->Keyword->Website->find('list',array("fields"=>array("Website.id","Website.website_name"),"conditions"=>array("Website.user_id"=>$this->Auth->User('id'))));
                  $this->set(compact('websites'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid KeywordHitLog.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('keywordHitLog', $this->KeywordHitLog->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->KeywordHitLog->create();
			if ($this->KeywordHitLog->save($this->data)) {
				$this->Session->setFlash(__('The KeywordHitLog has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The KeywordHitLog could not be saved. Please, try again.', true));
			}
		}
		$keywords = $this->KeywordHitLog->Keyword->find('list');
		$this->set(compact('keywords'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid KeywordHitLog', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->KeywordHitLog->save($this->data)) {
				$this->Session->setFlash(__('The KeywordHitLog has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The KeywordHitLog could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->KeywordHitLog->read(null, $id);
		}
		$keywords = $this->KeywordHitLog->Keyword->find('list');
		$this->set(compact('keywords'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for KeywordHitLog', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->KeywordHitLog->del($id)) {
			$this->Session->setFlash(__('KeywordHitLog deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>